package com.lancoo.answer.view.fragment.composition.continuation.sub;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.bean.ItemAsk;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.rxjava.rxlife.RxLife;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by pengmi on 2022/5/26 14:20
 * Description:读后续写文本+音频作答
 */
public class ContinuationAudioAnswerFragment extends Fragment {


    private Item item;
    private RecyclerView recyclerView;
    private int currentFocusIndex;
    private ContinuationAudioAnswerAdapter mAdapter;

    public static Fragment loadFragment(int typeIndex, int queseIndex, int childIndex){

        ContinuationAudioAnswerFragment fragment=new ContinuationAudioAnswerFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,typeIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,queseIndex);
        mBundle.putInt(AnswerConstant.KEY_CHILDINDEX,childIndex);
        fragment.setArguments(mBundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_fragment_composition_continuation_audio_answer_layout, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(getArguments()==null){
            return;
        }
        int typeIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        int queseIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        int childIndex = getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        if(ConstantBean.Companion.getConstantExamBean()==null){
            return;
        }
        List<Type> typeList = ConstantBean.Companion.getConstantExamBean().getTypeList();
        if(typeList==null||typeIndex>=typeList.size()){
            return;
        }
        Type type = typeList.get(typeIndex);
        List<Ques> quesList = type.getQuesList();
        if(quesList==null||quesList.size()<=queseIndex){
            return;
        }
        Ques ques = quesList.get(queseIndex);
        List<Child> childList = ques.getChildList();
        if(childList==null||childList.size()<=childIndex){
            return;
        }
        Child child = childList.get(childIndex);
        List<Item> itemList = child.getItemList();
        if(itemList==null||itemList.size()==0){
            return;
        }
        item = itemList.get(0);
        recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new ContinuationAudioAnswerAdapter(item);
        List<ItemAsk> itemAskList = item.getItemAskList();
        mAdapter.setCallBack(new ContinuationAudioAnswerAdapter.OnSubParagraphAnswerCallBack() {
            @Override
            public void updateAnswerCallBack() {
                super.updateAnswerCallBack();
                Observable.create((ObservableOnSubscribe<String>) emitter -> {
                            if (child.getItemList() == null || itemAskList == null) {
                                return;
                            }
                            StringBuilder stringBuilder = new StringBuilder();
                            for (ItemAsk itemAsk : itemAskList) {
                                if (!TextUtils.isEmpty(itemAsk.getStuAnswer())) {
                                    stringBuilder.append(itemAsk.getStuAnswer());
                                    stringBuilder.append(AnswerConstant.TRANSLATION_APPEND_CHAR);
                                }
                            }
                            child.getItemList().get(0).setStuAnswer(stringBuilder.toString().trim());
                            emitter.onNext("");
                        })
                        .subscribeOn(Schedulers.io()).subscribe();
            }

            @Override
            public void onClickItemIndex(int itemIndex) {
                super.onClickItemIndex(itemIndex);
                currentFocusIndex=itemIndex;
            }
        });
        recyclerView.setAdapter(mAdapter);
    }

    public void updateKeyboardShowState(boolean isShow){
        if(isShow&&recyclerView!=null){
            chooseClausesItemIndex(currentFocusIndex);
        }
    }

    /**
     * 选中下标itemIndex的分句作答
     * @param itemIndex index
     */
    public void chooseClausesItemIndex(int itemIndex){
        if(recyclerView!=null){
//            recyclerView.smoothScrollToPosition(itemIndex);

            Single.timer(200, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .as(RxLife.as(this))
                    .subscribe(aLong -> {
//                        recyclerView.smoothScrollToPosition(itemIndex);
                        mAdapter.localUpdate(itemIndex);
//                        Log.e("eee","读后续写 chooseClausesItemIndex "+itemIndex);
                    });
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if(mAdapter!=null){
            mAdapter.pauseMedia();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(mAdapter!=null){
            mAdapter.releaseMedia();
        }
    }
}
